package com.yh.controller;



import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.HttpServerErrorException;

import com.yh.bean.Dept;
import com.yh.bean.Permission;
import com.yh.bean.Role;
import com.yh.service.ManageService;
/*
 * 
 * 角色/权限 管理
 */
@Controller
@Scope("prototype")
@RequestMapping("/role")
public class RoleManageController {
	@Autowired
	private ManageService service;
	
	//新增角色
	@RequestMapping("/addRole")
	@ResponseBody
	public String addRole(Role role){
		
		int a=service.addRole(role);
		
		return "success";
	}
	
	//修改角色
	@RequestMapping("/updateRole")
	@ResponseBody
	public String updateRole(Role role){
		
		int a=service.updateRole(role);
		return "success";
	}
	
	//删除角色
	@RequestMapping("/delRole")
	@ResponseBody
	public String delRole(int roleid){
		int a=service.delRole(roleid);
		return "success";
	}
	
	//查询所有角色
	@RequestMapping("/queryRole")
	@ResponseBody
	public Map queryRole(int currentpage,int pagesize){
		System.out.println(currentpage);
		System.out.println(pagesize);
		Map map= service.queryListRole(currentpage,pagesize);
		return map;
		//return null;
	}
	
	//查询所有权限
	@RequestMapping("/queryPermission")
	@ResponseBody
	public Map listPermission(Role role){
		
		List<Permission> listPermission=service.queryListPermission(role);//获取已有权限
		role.setId(0);
		List<Permission> allPermission=service.queryListPermission(role);//获取全部权限
		Map map=new HashMap();
		map.put("listPermission", listPermission);
		map.put("allPermission", allPermission);
		//listPermission.forEach(ll->System.out.println(ll.toString()));
		//allPermission.forEach(ll->System.out.println(ll.toString()));
		return map;
		
	}
	
	//设置权限
	@RequestMapping("/updatePermission")
	@ResponseBody
	public String updatePermission(@RequestParam("arrperid[]")int[] arrperid,
			@RequestParam("arrpername[]")String[] arrpername,int roleid){
		
		System.out.println(arrperid);
		System.out.println(arrpername);
		System.out.println(roleid);
		
		//根据roleid删除该角色对应所有权限
		
		int a= service.updatePermission(arrperid, arrpername, roleid);
		//新增roleid 权限id，权限名字
		
		
		return "success";
	}
	
	
	/*//通过获取前端选中功能,更新数据库中当前角色的功能模块
	public List<Permission>  updatePermissions(String[] per,String role_id, HttpServletRequest req){

		Map map=new HashMap();
		map.put("per", per);
		map.put("role_id", role_id);
		service.addListPermission(map);
		
		
		return null;
	}*/
	
	
	
	
	
	
	
	
	
	
	
	
}
